// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_plan_api_serv

import (
	"context"
	"time"

	"gitcode.com/lvupclub/lvup-api/dao"
	"gitcode.com/lvupclub/lvup-api/dao/roadmap_dao"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_plan_api"
	"github.com/google/uuid"
)

func (apiImpl *RoadmapPlanApiImpl) AddPlanItem(ctx context.Context, req *roadmap_plan_api.AddPlanItemRequest) (*roadmap_plan_api.AddPlanItemResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.AddPlanItemResponse{
			Code:   roadmap_plan_api.AddPlanItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.AddPlanItemResponse{
			Code:   roadmap_plan_api.AddPlanItemResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	//只有路线图创建者有权限
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_plan_api.AddPlanItemResponse{
			Code:   roadmap_plan_api.AddPlanItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	planItem, err := roadmap_dao.PlanInfoDao.Get(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return &roadmap_plan_api.AddPlanItemResponse{
			Code:   roadmap_plan_api.AddPlanItemResponse_CODE_NO_PLAN,
			ErrMsg: "成长计划不存在",
		}, nil
	}

	if req.BasicInfo.StartTime < planItem.StartTime || req.BasicInfo.EndTime > planItem.EndTime || req.BasicInfo.StartTime > req.BasicInfo.EndTime {
		return &roadmap_plan_api.AddPlanItemResponse{
			Code:   roadmap_plan_api.AddPlanItemResponse_CODE_WRONG_TIME_RANGE,
			ErrMsg: "错误的时间区间",
		}, nil
	}

	itemId := uuid.NewString()
	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.PlanItemInfoDao.Insert(sessCtx, &roadmap_dao.PlanItemInfoDbItem{
			ItemId:    itemId,
			RoadmapId: req.RoadmapId,
			PlanId:    req.PlanId,
			BasicInfo: roadmap_dao.BasicPlanItemInfoDbItem{
				Title:     req.BasicInfo.Title,
				Content:   req.BasicInfo.Content,
				StartTime: req.BasicInfo.StartTime,
				EndTime:   req.BasicInfo.EndTime,
			},
			CreateTime: nowTime,
			UpdateTime: nowTime,
		})
		if err != nil {
			return nil, err
		}
		return &roadmap_plan_api.AddPlanItemResponse{
			Code:   roadmap_plan_api.AddPlanItemResponse_CODE_OK,
			ItemId: itemId,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_plan_api.AddPlanItemResponse), nil
}

func (apiImpl *RoadmapPlanApiImpl) UpdatePlanItem(ctx context.Context, req *roadmap_plan_api.UpdatePlanItemRequest) (*roadmap_plan_api.UpdatePlanItemResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code:   roadmap_plan_api.UpdatePlanItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code:   roadmap_plan_api.UpdatePlanItemResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	//只有路线图创建者有权限
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code:   roadmap_plan_api.UpdatePlanItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	planItem, err := roadmap_dao.PlanInfoDao.Get(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code:   roadmap_plan_api.UpdatePlanItemResponse_CODE_NO_PLAN,
			ErrMsg: "成长计划不存在",
		}, nil
	}

	if req.BasicInfo.StartTime < planItem.StartTime || req.BasicInfo.EndTime > planItem.EndTime || req.BasicInfo.StartTime > req.BasicInfo.EndTime {
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code:   roadmap_plan_api.UpdatePlanItemResponse_CODE_WRONG_TIME_RANGE,
			ErrMsg: "错误的时间区间",
		}, nil
	}
	exist, err := roadmap_dao.PlanItemInfoDao.Exist(ctx, req.RoadmapId, req.PlanId, req.ItemId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code:   roadmap_plan_api.UpdatePlanItemResponse_CODE_NO_PLAN_ITEM,
			ErrMsg: "成长任务不存在",
		}, nil
	}

	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.PlanItemInfoDao.UpdateBasic(sessCtx, req.RoadmapId, req.PlanId, req.ItemId, &roadmap_dao.BasicPlanItemInfoDbItem{
			Title:     req.BasicInfo.Title,
			Content:   req.BasicInfo.Content,
			StartTime: req.BasicInfo.StartTime,
			EndTime:   req.BasicInfo.EndTime,
		}, nowTime)
		if err != nil {
			return nil, err
		}
		return &roadmap_plan_api.UpdatePlanItemResponse{
			Code: roadmap_plan_api.UpdatePlanItemResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_plan_api.UpdatePlanItemResponse), nil
}

func (apiImpl *RoadmapPlanApiImpl) ListPlanItem(ctx context.Context, req *roadmap_plan_api.ListPlanItemRequest) (*roadmap_plan_api.ListPlanItemResponse, error) {
	emptyList := []*roadmap_plan_api.PlanItemInfo{}

	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.ListPlanItemResponse{
			Code:     roadmap_plan_api.ListPlanItemResponse_CODE_WRONG_SESSION,
			ErrMsg:   "会话不存在",
			ItemList: emptyList,
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.ListPlanItemResponse{
			Code:     roadmap_plan_api.ListPlanItemResponse_CODE_NO_ROADMAP,
			ErrMsg:   "成长路线图不存在",
			ItemList: emptyList,
		}, nil
	}
	if roadmapItem.OwnerUserId == sessItem.UserId {
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.ListPlanItemResponse{
				Code:     roadmap_plan_api.ListPlanItemResponse_CODE_NO_TARGET_MEMBER,
				ErrMsg:   "目标成员不存在",
				ItemList: emptyList,
			}, nil
		}
	} else {
		if sessItem.UserId != req.TargetMemberUserId {
			return &roadmap_plan_api.ListPlanItemResponse{
				Code:     roadmap_plan_api.ListPlanItemResponse_CODE_NO_PERMISSION,
				ErrMsg:   "没有权限",
				ItemList: emptyList,
			}, nil
		}
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.ListPlanItemResponse{
				Code:     roadmap_plan_api.ListPlanItemResponse_CODE_NO_PERMISSION,
				ErrMsg:   "没有权限",
				ItemList: emptyList,
			}, nil
		}
	}

	exist, err := roadmap_dao.PlanInfoDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.ListPlanItemResponse{
			Code:     roadmap_plan_api.ListPlanItemResponse_CODE_NO_PLAN,
			ErrMsg:   "成长计划不存在",
			ItemList: emptyList,
		}, nil
	}

	itemItemList, err := roadmap_dao.PlanItemInfoDao.List(ctx, req.RoadmapId, req.PlanId)
	if err != nil {
		return nil, err
	}
	itemList := []*roadmap_plan_api.PlanItemInfo{}
	for _, itemItem := range itemItemList {
		itemList = append(itemList, itemItem.ToPlanItemInfo())
	}

	return &roadmap_plan_api.ListPlanItemResponse{
		Code:     roadmap_plan_api.ListPlanItemResponse_CODE_OK,
		ItemList: itemList,
	}, nil
}

func (apiImpl *RoadmapPlanApiImpl) GetPlanItem(ctx context.Context, req *roadmap_plan_api.GetPlanItemRequest) (*roadmap_plan_api.GetPlanItemResponse, error) {
	emptyInfo := &roadmap_plan_api.PlanItemInfo{
		BasicInfo: &roadmap_plan_api.BasicPlanItemInfo{},
	}

	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.GetPlanItemResponse{
			Code:   roadmap_plan_api.GetPlanItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
			Item:   emptyInfo,
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.GetPlanItemResponse{
			Code:   roadmap_plan_api.GetPlanItemResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
			Item:   emptyInfo,
		}, nil
	}
	if roadmapItem.OwnerUserId == sessItem.UserId {
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.GetPlanItemResponse{
				Code:   roadmap_plan_api.GetPlanItemResponse_CODE_NO_TARGET_MEMBER,
				ErrMsg: "目标成员不存在",
				Item:   emptyInfo,
			}, nil
		}
	} else {
		if sessItem.UserId != req.TargetMemberUserId {
			return &roadmap_plan_api.GetPlanItemResponse{
				Code:   roadmap_plan_api.GetPlanItemResponse_CODE_NO_PERMISSION,
				ErrMsg: "没有权限",
				Item:   emptyInfo,
			}, nil
		}
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.GetPlanItemResponse{
				Code:   roadmap_plan_api.GetPlanItemResponse_CODE_NO_PERMISSION,
				ErrMsg: "没有权限",
				Item:   emptyInfo,
			}, nil
		}
	}
	exist, err := roadmap_dao.PlanInfoDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.GetPlanItemResponse{
			Code:   roadmap_plan_api.GetPlanItemResponse_CODE_NO_PLAN,
			ErrMsg: "成长计划不存在",
			Item:   emptyInfo,
		}, nil
	}

	itemItem, err := roadmap_dao.PlanItemInfoDao.Get(ctx, req.RoadmapId, req.PlanId, req.ItemId)
	if err != nil {
		return &roadmap_plan_api.GetPlanItemResponse{
			Code:   roadmap_plan_api.GetPlanItemResponse_CODE_NO_PLAN_ITEM,
			ErrMsg: "成长任务不存在",
			Item:   emptyInfo,
		}, nil
	}

	return &roadmap_plan_api.GetPlanItemResponse{
		Code: roadmap_plan_api.GetPlanItemResponse_CODE_OK,
		Item: itemItem.ToPlanItemInfo(),
	}, nil
}

func (apiImpl *RoadmapPlanApiImpl) RemovePlanItem(ctx context.Context, req *roadmap_plan_api.RemovePlanItemRequest) (*roadmap_plan_api.RemovePlanItemResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.RemovePlanItemResponse{
			Code:   roadmap_plan_api.RemovePlanItemResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.RemovePlanItemResponse{
			Code:   roadmap_plan_api.RemovePlanItemResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	//只有路线图创建者有权限
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_plan_api.RemovePlanItemResponse{
			Code:   roadmap_plan_api.RemovePlanItemResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	exist, err := roadmap_dao.PlanItemInfoDao.Exist(ctx, req.RoadmapId, req.PlanId, req.ItemId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.RemovePlanItemResponse{
			Code: roadmap_plan_api.RemovePlanItemResponse_CODE_OK,
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.PlanItemInfoDao.Remove(sessCtx, req.RoadmapId, req.PlanId, req.ItemId)
		if err != nil {
			return nil, err
		}
		return &roadmap_plan_api.RemovePlanItemResponse{
			Code: roadmap_plan_api.RemovePlanItemResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_plan_api.RemovePlanItemResponse), nil
}
